﻿using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using LanDBom.Controllers;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace LanDBom.Models.Procure
{
    public class Find_All_GYS
    {
        LDbaseEntities LD = new LDbaseEntities();
        public List<Providers> find_GYS_bjxt(string button_te)
        {
            List<Providers> pr = LD.Providers.Where(x => x.PR_bei == "临时").ToList();
            
            if (button_te != "" && button_te != null)
            {
                pr = pr.Where(x => x.PR_name.Contains(button_te)).ToList();
            }
            return pr;
        }
        public List<Providers> Find_GYS(string button_te, string HH)//hh----bomini的id
        {
            try
            {
                HH = HH == "" ? "-3" : HH;
                var bok = LD.BOMini.Find(int.Parse(HH));
                var biansj = bok == null ? "" : bok.BOM2_ST;
                List<Providers> fom = button_te == null ? LD.Providers.Where(s => s.PR_bei == "准入").Take(30).ToList() : LD.Providers.Where(s => s.PR_name == button_te && s.PR_bei == "准入" && (s.PR_Lei1.Contains(biansj) || s.PR_Lei2.Contains(biansj))).Take(30).ToList();
                //计算集成
                //
                //集成查询客户
                var client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "BD_Supplier";
                string fieldKeys = "FNumber,FName,FAddress,FMobile,FEMail,FContactDescription";//编号 名称 地址 联系电话 邮箱 备注
                string Filter = "FName like'%" + button_te + "%'";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = Filter,
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 20,
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());
                //返回的数据类型为List<List<object>>，错误的返回结果只有一行数据且IsSuccess为False
                if (result.Count == 1)
                {
                    //对返回结果进行解析和校验，这里使用的是JsonPatch
                    var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                    var queryNode = resultJObject.SelectToken("$..IsSuccess");
                    if (queryNode != null)
                    {
                        var isSuccess = queryNode.Value<Boolean>();
                        Info.AppendLine(isSuccess ? "操作成功" : "操作失败");
                    }
                    else
                    {
                        ApplicationController.JicheGong(fom, result);
                        Info.AppendLine("操作成功");
                    }
                }
                else
                {
                    ApplicationController.JicheGong(fom, result);
                    Info.AppendLine("操作成功");
                }

                return fom;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Providers> Find_ZHIding(string button_te, string HH)//输入文本 bomini 
        {
            try
            {
                HH = HH == "" ? "-3" : HH;
                var bok = LD.BOMini.Find(int.Parse(HH));
                var biansj = bok == null ? "" : bok.BOM2_ST;
                var biansj1 = bok == null ? "" : bok.BOM2_Sone;
                if (biansj1 == "3")
                {
                    var shund = biansj.Split(new char[2] { '（', '）' });
                    if (shund.Length==1)
                    {
                        biansj = shund[0];
                    }else
                    {
                        biansj = shund[1];
                    }
                }
                if (biansj== "礼品盒")
                {
                    biansj = "彩印纸箱";
                }
                if(biansj == "气泡袋")
                {
                    biansj = "膜/袋";
                }
                List<Providers> fom =LD.Providers.Where(s => s.PR_name.Contains(button_te) && s.PR_bei == "准入" && (s.PR_Lei1.Contains(biansj) || s.PR_Lei2.Contains(biansj))).Take(30).ToList();
                //计算集成
                //
                //集成查询客户
                var client = new K3CloudApi();
                
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "BAS_PreBaseDataTwo";
                string fieldKeys = "F_land_Base.FNumber,F_land_BaseProperty";//编号 名称 
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FDocumentStatus<>'Z' and FForbidStatus='A' and F_SMGS_WLFZ.FName like'%" + biansj + "%' and F_land_BaseProperty like'%" + button_te + "%' and FUseOrgId.FNumber='10.01'",
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 200,
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());
                //返回的数据类型为List<List<object>>，错误的返回结果只有一行数据且IsSuccess为False
                if (result.Count == 1)
                {
                    //对返回结果进行解析和校验，这里使用的是JsonPatch
                    var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                    var queryNode = resultJObject.SelectToken("$..IsSuccess");
                    if (queryNode != null)
                    {
                        var isSuccess = queryNode.Value<Boolean>();
                        Info.AppendLine(isSuccess ? "操作成功" : "操作失败");
                    }
                    else
                    {
                        ApplicationController.JicheGong(fom, result);
                        Info.AppendLine("操作成功");
                    }
                }
                else
                {
                    ApplicationController.JicheGong(fom, result);
                    Info.AppendLine("操作成功");
                }
                var shunf =new List<Providers>();
                foreach (var item in fom)
                {
                    var J = item.PR_name;
                    var S = shunf.Where(s => s.PR_name == J).FirstOrDefault();
                    if (S==null)
                    {
                        shunf.Add(item);
                    }
                }

                return shunf;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}